package com.tanhua.app.controller;

import com.aliyuncs.exceptions.ClientException;
import com.tanhua.app.interceptor.UserHolder;
import com.tanhua.app.service.VideoMqService;
import com.tanhua.app.service.VideoService;
import com.tanhua.domain.vo.PageBeanVo;
import org.apache.rocketmq.common.stats.MomentStatsItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
public class VideoController {


    @Autowired
    private VideoService videoService;
    @Autowired
    private VideoMqService videoMqService;


    @GetMapping(value = "/smallVideos", name = "查询视频列表动态")
    public ResponseEntity findVideo(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pagesize) throws ClientException, IOException {
        if (page < 1) {
            page = 1;
        }


        PageBeanVo pb = videoService.findVideo(page, pagesize);

        return ResponseEntity.ok(pb);
    }

    @PostMapping(value = "/smallVideos", name = "小视频的发布")
    public ResponseEntity saveVideo(MultipartFile videoThumbnail, MultipartFile videoFile) throws Exception {
        // 往消息中间件中存数据 （type=2）
        videoMqService.sendMsg(UserHolder.getUser().getId(), 1);
        return videoService.saveVideo(videoThumbnail, videoFile);
    }

    @PostMapping(value = "/smallVideos/{uid}/userFocus", name = "小视频的作者关注")
    public ResponseEntity saveGz(@PathVariable("uid") Long uid) throws Exception {
        // 往消息中间件中存数据 （type=100）
        videoMqService.sendMsg2(uid, 2);
        return videoService.saveGz(uid);
    }

    @PostMapping(value = "/smallVideos/{uid}/userUnFocus", name = "小视频的作者撤销关注")
    public ResponseEntity removeGz(@PathVariable("uid") Long uid) throws Exception {
        videoMqService.sendMsg2(uid, 3);
        return videoService.removeGz(uid);
    }


    @PostMapping(value = "/smallVideos/{id}/dislike", name = "小视频点赞")
    public ResponseEntity likeVideo(@PathVariable("id") String publishId)  {
        // 往消息中间件中存数据 （type=5）
        videoMqService.sendMsg(publishId, 4);
        return videoService.likeVideo(publishId);
    }

    @PostMapping(value = "/smallVideos/{id}/like", name = "小视频取消点赞")
    public ResponseEntity dislikeVideo(@PathVariable("id") String publishId)  {
        // 往消息中间件中存数据 （type=-5）
        videoMqService.sendMsg(publishId, 5);
        return videoService.dislikeVideo(publishId);
    }

    @GetMapping(value = "/smallVideos/{id}/comments",name = "查询评论列表视屏")
    public ResponseEntity findplList(@RequestParam(defaultValue = "1") Integer page,
                                     @RequestParam(defaultValue = "10") Integer pagesize,
                                     @PathVariable("id") String publishId) {
        return videoService.findplList(page,pagesize,publishId);
    }

    @PostMapping(value = "/smallVideos/{id}/comments",name = "发布评论")
    public ResponseEntity savepl(@RequestBody Map param,
                                 @PathVariable("id") String publishId) {
        String text = (String) param.get("comment");
        // 往消息中间件中存数据 （type=10）
        videoMqService.sendMsg(publishId, 6);
        return videoService.savepl(text,publishId);
    }

    @PostMapping(value = "/smallVideos/comments/{id}/like", name = "评论点赞")
    public ResponseEntity likeComments(@PathVariable("id") String publishId)  {
        return videoService.likeComments(publishId);
    }

    @PostMapping(value = "/smallVideos/comments/{id}/dislike", name = "评论取消点赞")
    public ResponseEntity dislikeComments(@PathVariable("id") String publishId)  {
        return videoService.dislikeComments(publishId);
    }
}
